package tw.edu.ttu.cse.FourCLab;

import android.app.Activity;
import android.view.View;
import android.view.View.*;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.widget.*;

public class SystemPreferences extends Activity {
	public static final String PREFS_NAME = "MyPrefsFile";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.system_preferences);
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        String savedPreference = settings.getString("StringPreference", "Default Preference");
        EditText preferenceComponent = (EditText)findViewById(R.id.SharePreference);
        preferenceComponent.setText(savedPreference);
        Button changeButton = (Button)findViewById(R.id.Change);
        changeButton.setOnClickListener(changeClickListener);
        Button showDefaultButton = (Button)findViewById(R.id.ShowSavedPreference);
        showDefaultButton.setOnClickListener(showSavedClickListener);
    }
    
    private OnClickListener changeClickListener = new OnClickListener() {
        public void onClick(View v) {
        	SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        	SharedPreferences.Editor editor = settings.edit();
        	String textBoxValue = ((EditText)findViewById(R.id.SharePreference)).getText().toString();
        	editor.putString("StringPreference", textBoxValue);
        	editor.commit();
        }
    };
    
    private OnClickListener showSavedClickListener = new OnClickListener() {
        public void onClick(View v) {
        	SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        	String savedPreference = settings.getString("StringPreference", "Default Preference");
            EditText preferenceComponent = (EditText)findViewById(R.id.SharePreference);
            preferenceComponent.setText(savedPreference);
        }
    };
    

}